package com.gexiaobao.pigeon.ui.fragment.mine.tools.query

import android.os.Bundle
import android.text.InputType
import android.view.View
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentSearchPigeonBinding
import com.gexiaobao.pigeon.viewmodel.MineToolsViwModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/8/3 11:16
 * @Describe    : 查赛鸽
 */
class FragmentSearchPigeon : BaseFragment<MineToolsViwModel, FragmentSearchPigeonBinding>() {

    private var mType: Int? = 0

    override fun initView(savedInstanceState: Bundle?) {
        mType = arguments?.getInt("type")
        when (mType) {
            1 -> {
                mDatabind.llTop.visibility = View.VISIBLE
                mDatabind.etFindPigeonRingNo.hint = "输入足环号"
            }

            2 -> {
                mDatabind.llTop.visibility = View.GONE
                mDatabind.etFindPigeonRingNo.hint = "输入参赛名"
                mDatabind.etFindPigeonRingNo.inputType = InputType.TYPE_CLASS_TEXT
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvFindPigeon) {
            when (it) {
                mDatabind.tvFindPigeon -> {
                    val ringNo = mDatabind.etFindPigeonRingNo.text.toString().trim()
                    val ringYear =
                        if (mDatabind.etRingNoYear.text.toString().trim().isEmpty()) "" else mDatabind.etRingNoYear.text.toString().trim() + "-"
                    val ringQ = if (mDatabind.etRingNoQh.text.toString().trim().isEmpty()) "" else mDatabind.etRingNoQh.text.toString()
                        .trim() + "-"
                    when (mType) {
                        1 -> {
                            if (ringNo.isEmpty()) {
                                RxToast.showToast("请输入足环号")
                                return@setOnclickNoRepeat
                            }
                            nav().navigateAction(R.id.action_to_fragment_search_ring_no, Bundle().apply {
                                putString("ringNumber", "$ringYear$ringQ$ringNo")
                            })
                        }

                        2 -> {
                            if (ringNo.isEmpty()) {
                                RxToast.showToast("请输入参赛名")
                                return@setOnclickNoRepeat
                            }
                            nav().navigateAction(R.id.action_to_fragment_search_name, Bundle().apply {
                                putString("masterName", ringNo)
                            })
                        }
                    }
                }
            }
        }
    }

    companion object {
        fun newInstance(type: Int): FragmentSearchPigeon {
            val fragment = FragmentSearchPigeon()
            val bundle = Bundle()
            bundle.putInt("type", type)
            fragment.arguments = bundle
            return fragment
        }
    }
}